﻿@{
    ViewData["Title"] = "简易代码生成器";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-card">
        <div class="layui-card-header">简易代码生成器</div>
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">中文表名</label>
                    <div class="layui-input-block">
                        <input type="text" value="测试表" name="title" autocomplete="off" placeholder="请输入中文表名" lay-verify="required" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">中文字段（多行用回车或逗号分割）</label>
                    <div class="layui-input-block">
                        <textarea name="fileds" placeholder="请输入中文字段" lay-verify="required" class="layui-textarea">中文,字段（多,行,用回,车或逗,号分,割）</textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn" lay-submit lay-filter="btnMake">生成</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-tab layui-tab-brief">
                <ul class="layui-tab-title">
                    <li class="layui-this">实体类</li>
                    <li>输入类</li>
                    <li>输出类</li>
                    <li>服务类</li>
                    <li>列表页面</li>
                    <li>编辑页面</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <pre id="layui-code-1">
                            <textarea id="view-1" class="layui-hide"></textarea>   
                        </pre>
                    </div>
                    <div class="layui-tab-item">
                        <pre id="layui-code-2">
                            <textarea id="view-2" class="layui-hide"></textarea>   
                        </pre>
                    </div>
                    <div class="layui-tab-item">
                        <pre id="layui-code-3">
                            <textarea id="view-3" class="layui-hide"></textarea>   
                        </pre>
                    </div>
                    <div class="layui-tab-item">
                        <pre id="layui-code-4">
                            <textarea id="view-4" class="layui-hide"></textarea>   
                        </pre>
                    </div>
                    <div class="layui-tab-item">
                        <pre id="layui-code-5">
                            <textarea id="view-5" class="layui-hide"></textarea>   
                        </pre>
                    </div>
                     <div class="layui-tab-item">
                        <pre id="layui-code-6">
                            <textarea id="view-6" class="layui-hide"></textarea>   
                        </pre>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script id="TPL" type="text/html">
     {{#  layui.each(d.list, function(index, item){ }}
     ///{{= item.alias }}
     {{= item.modname }}
     ///{{= item.alias }}
    {{#  }); }}
</script>

@section scripts {
    <script src="/component/libs/ChinesePY.js?v=3"></script>
    <script type="text/javascript">
        var app = {};
        
        layui.use(['toast'], function () {
            var $ = layui.$, form = layui.form, laytpl = layui.laytpl, toast = layui.toast;
            //
            app.init = function () {

                form.on('submit(btnMake)', function (data) {
                    //按回车或逗号拆分字符串为数组
                    var arr = data.field.fileds.split(',');
                    var entity = {
                        bm: data.field.title,
                        bmpy: Pinyin.GetZD(data.field.title),
                        zd: []
                    };
                    arr.forEach((item) => {
                        entity.zd.push({ 'mc': item, 'py': Pinyin.GetZD(item) })
                    })
                    //
                    app.createEntity(entity);
                    app.createInput(entity);
                    app.createOutput(entity);
                    app.createService(entity);
                    app.createListView(entity);
                    app.createAddView(entity);
                    //
                    return false;
                });
            }();
            //
            app.createEntity = function (en) {
                var txt = '//版权所有 (c) 2021-2024 duanzt\n\n'

                txt += 'namespace Admin.NET.Plugin.XXX;\n';
                txt += '/// <summary>\n'
                txt += '/// ' + en.bm + '\n'
                txt += '/// </summary>\n'
                txt += '[SugarTable(null, "' + en.bm + '")]\n'
                txt += 'public class ' + en.bmpy + ' : EntityTenant\n'
                txt += '{\n'
                en.zd.forEach((item) => {
                    txt += '    /// <summary>\n'
                    txt += '    /// ' + item.mc + '\n'
                    txt += '    /// </summary>\n'
                    txt += '    [SugarColumn(ColumnDescription = "' + item.mc + '")] \n'
                    txt += '    public string ' + item.py + ' { get; set; } \n\n'
                });
                txt += '}\n'

                $('#layui-code-1').html('<textarea id="view-1" class="layui-hide"></textarea>');
                $('#view-1').val(txt);
                layui.code({ elem: '#layui-code-1' });
            }
            //
            app.createInput = function (en) {
                var bmpy = en.bmpy;
                var txt = '//版权所有 (c) 2021-2024 duanzt\n\n'

                txt += 'namespace Admin.NET.Plugin.XXX.Service.xxx.Dto;\n'
                txt += '/// <summary>\n'
                txt += '/// ' + en.bm + '\n'
                txt += '/// </summary>\n'
                txt += 'public class ' + bmpy + 'Input : BasePageInput\n'
                txt += '{\n'

                en.zd.forEach((item) => {
                    txt += '    /// <summary>\n'
                    txt += '    /// ' + item.mc +'\n'
                    txt += '    /// </summary>\n'
                    txt += '    public virtual string ' + item.py + ' { get; set; }\n'
                });
                txt += '}\n'

                txt += 'public class Add' + bmpy + 'Input : ' + bmpy + '\n'
                txt += '{\n'

                txt += '}\n'

                txt += 'public class Update' + bmpy + 'Input : Add' + bmpy + 'Input\n'
                txt += '{\n'
                txt += '}\n'

                txt += 'public class Delete' + bmpy + 'Input : BaseIdInput\n'
                txt += '{\n'
                txt += '}\n'

                $('#layui-code-2').html('<textarea id="view-2" class="layui-hide"></textarea>');
                $('#view-2').val(txt);
                layui.code({ elem: '#layui-code-2' });
            }
            //
            app.createOutput = function (en) {
                var txt = '//版权所有 (c) 2021-2024 duanzt\n\n'

                txt += 'namespace Admin.NET.Plugin.XXX;\n';
                txt += '/// <summary>\n'
                txt += '/// ' + en.bm + '\n'
                txt += '/// </summary>\n'
                txt += 'public class ' + en.bmpy + 'Output\n'
                txt += '{\n'

                txt += '    /// <summary>\n'
                txt += '    /// Id\n'
                txt += '    /// </summary>\n'
                txt += '    public long Id { get; set; }\n\n'
                en.zd.forEach((item) => {
                    txt += '    /// <summary>\n'
                    txt += '    /// ' + item.mc + '\n'
                    txt += '    /// </summary>\n'
                    txt += '    public string ' + item.py + ' { get; set; } \n\n'
                });
                txt += '}\n'

                $('#layui-code-3').html('<textarea id="view-3" class="layui-hide-3"></textarea>');
                $('#view-3').val(txt);
                layui.code({ elem: '#layui-code-3' });
            }
            //
            app.createService = function (en) {
                var bmpy = en.bmpy;
                var bm = en.bm;
                var txt = '//版权所有 (c) 2021-2024 duanzt\n\n'

                txt += 'namespace Admin.NET.Core.Service;\n'
                txt += '/// <summary>\n'
                txt += '/// ' + bm + '服务 \n'
                txt += '/// </summary>\n'
                txt += '[ApiDescriptionSettings(Order = 460)]\n'
                txt += 'public class ' + bmpy + 'Service : IDynamicApiController, ITransient\n'
                txt += '{\n'
                txt += '    private readonly SqlSugarRepository<' + bmpy + '> _rep;\n'

                txt += '    public ' + bmpy + 'Service(SqlSugarRepository<' + bmpy + '> ' + bmpy + 'Rep)\n'
                txt += '    {\n'
                txt += '        _rep = ' + bmpy + 'Rep;\n'
                txt += '    }\n'


                txt += '    /// <summary>\n'
                txt += '    /// 获取' + bm + '分页列表 \n'
                txt += '    /// </summary>\n'
                txt += '    /// <param name="input"></param>\n'
                txt += '    /// <returns></returns>\n'
                txt += '    [DisplayName("获取' + bm + '分页列表")]\n'
                txt += '    public async Task<List<' + bmpy + '>> GetList(PageRoleInput input)\n'
                txt += '    {\n'
                txt += '	return await _rep.AsQueryable()\n'
                en.zd.forEach((item) => {
                    txt += '		.WhereIF(!string.IsNullOrWhiteSpace(input.' + item.py + '), u => u.' + item.py + '.Contains(input.' + item.py + '))\n'
                });
                txt += '		.OrderBy(u => u.Id)\n'
                txt += '		.ToPagedListAsync(input.Page, input.PageSize);\n'
                txt += '    }\n\n'

                txt += '    /// <summary>\n'
                txt += '    /// 增加' + bm + ' \n'
                txt += '    /// </summary>\n'
                txt += '    /// <param name="input"></param>\n'
                txt += '    /// <returns></returns>\n'
                txt += '    [ApiDescriptionSettings(Name = "Add"), HttpPost]\n'
                txt += '    [DisplayName("增加' + bm + '")]\n'
                txt += '    public async Task Add' + bmpy + '(Add' + bmpy + 'Input input)\n'
                txt += '    {\n'
                txt += '        await _rep.InsertAsync(input.Adapt<' + bmpy + '>());\n'
                txt += '    }\n\n'

                txt += '    /// <summary>\n'
                txt += '    /// 更新' + bm + ' \n'
                txt += '    /// </summary>\n'
                txt += '    /// <param name="input"></param>\n'
                txt += '    /// <returns></returns>\n'
                txt += '    [ApiDescriptionSettings(Name = "Update"), HttpPost]\n'
                txt += '    [DisplayName("更新' + bm + '")]\n'
                txt += '    public async Task Update' + bmpy + '(Update' + bmpy + 'Input input)\n'
                txt += '    {\n'
                txt += '        await _rep.AsUpdateable(input.Adapt<' + bmpy + '>()).IgnoreColumns(true).ExecuteCommandAsync();\n'
                txt += '    }\n\n'

                txt += '    /// <summary>\n'
                txt += '    /// 删除' + bm + ' \n'
                txt += '    /// </summary>\n'
                txt += '    /// <param name="input"></param>\n'
                txt += '    /// <returns></returns>\n'
                txt += '    [ApiDescriptionSettings(Name = "Delete"), HttpPost]\n'
                txt += '    [DisplayName("删除' + bm + '")]\n'
                txt += '    public async Task Delete' + bmpy + '(Delete' + bmpy + 'Input input)\n'
                txt += '    {\n'
                txt += '        await _rep.DeleteAsync(u => u.Id == input.Id);\n'
                txt += '    }\n\n'
                txt += '}\n'

                $('#layui-code-4').html('<textarea id="view-4" class="layui-hide"></textarea>');
                $('#view-4').val(txt);
                layui.code({ elem: '#layui-code-4' });
            }
            //
            app.createListView = function (en) {
                var bm = en.bm;
                var folder = Pinyin.GetSZMXX(en.bmpy);
                var txt = '';

                txt += '&#64;{\n';
                txt += '    ViewData["Title"] = "' + bm + '列表";\n';
                txt += '}\n';
                txt += '<div class="layui-layout layui-layout-admin">\n';
                txt += '    <div class="layui-card">\n';
                txt += '        <div class="layui-card-body">\n';
                txt += '            <form class="layui-form layui-form-pane">\n';
                txt += '                <div class="layui-form-item">\n';
                txt += '                    <label class="layui-form-label">名称</label>\n';
                txt += '                    <div class="layui-input-inline">\n';
                txt += '                        <input type="text" name="name" autocomplete="off" class="layui-input">\n';
                txt += '                    </div>\n';
                txt += '                    <div class="layui-inline">\n';
                txt += '                        <button type="submit" class="layui-btn" lay-submit lay-filter="btnsearch">\n';
                txt += '                            <i class="layui-icon layui-icon-search"></i> 查询\n';
                txt += '                        </button>\n';
                txt += '                    </div>\n';
                txt += '                </div>\n';
                txt += '            </form>\n';

                txt += '            <table id="lsttable" lay-filter="lsttable"></table>\n';
                txt += '            <' + 'script type="text/html" id="toolbar">\n';
                txt += '                <button class="layui-btn layui-btn-sm" lay-event="add">\n';
                txt += '                    <i class="layui-icon layui-icon-addition"></i> 添加\n';
                txt += '                </button>\n';
                txt += '            <' + '/script' +'>\n';
                txt += '            <' + 'script type="text/html" id="bar">\n';
                txt += '                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="edit">编辑</a>\n';
                txt += '                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="del">删除</a>\n';
                txt += '            <' +'/script' +'>\n';
                txt += '        </div>\n';
                txt += '    </div>\n';
                txt += '</div>\n';

                txt += '&#64;section scripts {\n';
                txt += '    <' +'script type="text/javascript">\n';
                txt += '        var app = {};\n';
                txt += '        layui.use(["tools", "toast", "http"], function () {\n';
                txt += '            var table = layui.table,\n';
                txt += '                $ = layui.$,\n';
                txt += '                form = layui.form,\n';
                txt += '                http = layui.http,\n';
                txt += '                toast = layui.toast,\n';
                txt += '                tools = layui.tools;\n';

                txt += '            app.init = function () {\n';
                txt += '                //\n';
                txt += '                form.on("submit(btnsearch)", function (data) {\n';
                txt += '                    console.log(data.field)\n';
                txt += '                    table.reload("lsttable", {\n';
                txt += '                        where: data.field\n';
                txt += '                    })\n';
                txt += '                    return false;\n';
                txt += '                });\n';
                txt += '                //\n';
                txt += '                let cols =\n';
                txt += '                    [[\n';
                txt += '                        { checkbox: true, fixed: true },\n';
                en.zd.forEach((item) => {
                txt += '                        { title: "' + item.mc + '", field: "' +  Pinyin.GetSZMXX(item.py) + '", width: 160,sort: true },\n';
                });
                txt += '                        { fixed: "right", title: "操作", toolbar: "#bar", align: "right", width: 120 }\n';
                txt += '                    ]];\n';
                txt += '                //\n';
                txt += '                tools.PageTable({ url: "/api/' + folder + '/page", cols });\n';
                txt += '                table.on("toolbar(lsttable)", function (obj) {\n';
                txt += '                    var id = obj.config.id;\n';
                txt += '                    var checkStatus = table.checkStatus(id);\n';
                txt += '                    switch (obj.event) {\n';
                txt += '                        case "add":\n';
                txt += '                            app.add();\n';
                txt += '                            break;\n';
                txt += '                    };\n';
                txt += '                });\n';
                txt += '                table.on("tool(lsttable)", function (obj) {\n';
                txt += '                    var data = obj.data;\n';
                txt += '                    switch (obj.event) {\n';
                txt += '                        case "edit":\n';
                txt += '                            app.add(data);\n';
                txt += '                            break;\n';
                txt += '                        case "del":\n';
                txt += '                            app.del(data.id);\n';
                txt += '                            break;\n';
                txt += '                    };\n';
                txt += '                });\n';
                txt += '            }();\n';
                txt += '            //\n';
                txt += '            app.add = function (data) {\n';
                txt += '                data = data || {};\n';
                txt += '                tools.open({\n';
                txt += '                    area: ["700px", "100%"],\n';
                txt += '                    content: "./add",\n';
                txt += '                    title: "' + bm + '管理",\n';
                txt += '                    success: function (layero, index, that) {\n';
                txt += '                        var iframeWin = window[layero.find("iframe")[0]["name"]];\n';
                txt += '                        iframeWin.app.data = data\n';
                txt += '                    },\n';
                txt += '                    yes: function (index, layero) {\n';
                txt += '                        var iframeWin = window[layero.find("iframe")[0]["name"]];\n';
                txt += '                        iframeWin.app.save();\n';
                txt += '                    }\n';
                txt += '                });\n';
                txt += '            }\n';
                txt += '            app.del = function (id) {\n';
                txt += '                var index = layer.confirm("您确定要删除当前' + bm + '吗？", function () {\n';
                txt += '                    http.post("/api/' + folder + '/delete", { id }).then((res) => {\n';
                txt += '                        if (res.code == 200) {\n';
                txt += '                            layer.close(index);\n';
                txt += '                            table.reload("lsttable");\n';
                txt += '                            toast.success({ message: "删除成功！" });\n';
                txt += '                        }\n';
                txt += '                        else {\n';
                txt += '                            layer.alert("删除失败，错误信息" + res.message, { icon: 2 });\n';
                txt += '                        }\n';
                txt += '                    })\n';
                txt += '                });\n';
                txt += '            }\n';
                txt += '        })\n';
                txt += '    <' +'/script' + '>\n';
                txt += '}\n';

                $('#layui-code-5').html('<textarea id="view-5" class="layui-hide"></textarea>');
                $('#view-5').val(txt);
                layui.code({ elem: '#layui-code-5' });
            }

            //
            app.createAddView = function (en) {
                var folder = Pinyin.GetSZMXX(en.bmpy);
                var txt = '';

                txt += '&#64;{\n';
                txt += '    ViewData["Title"] = "' + en.bm +'管理";\n';
                txt += '}\n';
                txt += '<div class="layui-layout layui-layout-admin">\n';
                txt += '    <form class="layui-form" action="" lay-filter="val-filter">\n';
                en.zd.forEach((item) => {
                txt += '        <div class="layui-form-item">\n';
                txt += '            <label class="layui-form-label">' + item.mc +'</label>\n';
                txt += '            <div class="layui-input-block">\n';
                txt += '                <input type="hidden" name="id" value="0">\n';
                txt += '                <input type="text" name="' + Pinyin.GetSZMXX(item.py) +'" autocomplete="off" lay-verify="required" class="layui-input">\n';
                txt += '            </div>\n';
                txt += '        </div>\n';
                });
                txt += '    </form>\n';
                txt += '</div>\n';

                txt += '&#64;section scripts {\n';
                txt += '    <script type="text/javascript">\n';
                txt += '        var app = {};\n';
                txt += '        layui.use(["http", "toast"], function () {\n';
                txt += '            var form = layui.form,\n';
                txt += '                toast = layui.toast,\n';
                txt += '                http = layui.http;\n';
                txt += '            //\n';
                txt += '            app.init = function () {\n';
                txt += '                var data = app.data || {};\n';
                txt += '                form.val("val-filter", data);\n';
                txt += '            }();\n';
                txt += '            app.save = function () {\n';
                txt += '                form.submit("val-filter", function (data) {\n';
                txt += '                    var postData = data.field;\n';
                txt += '                    //\n';
                txt += '                    var url = postData.id == "0" ? "/api/' + folder + '/add" : "/api/' + folder + '/update"\n';
                txt += '                    http.post(url, postData).then((res) => {\n';
                txt += '                        if (res.code == 200) {\n';
                txt += '                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页\n';
                txt += '                            parent.layui.table.reload("lsttable");\n';
                txt += '                            toast.success({ message: "保存成功！" });\n';
                txt += '                        }\n';
                txt += '                        else {\n';
                txt += '                            layer.alert("保存失败，错误信息:" + res.message, { icon: 2 });\n';
                txt += '                        }\n';
                txt += '                    });\n';
                txt += '                    return false;\n';
                txt += '                });\n';
                txt += '            }\n';
                txt += '        })\n';
                txt += '    <' + '/script>\n';
                txt += '}\n';


                $('#layui-code-6').html('<textarea id="view-6" class="layui-hide"></textarea>');
                $('#view-6').val(txt);
                layui.code({ elem: '#layui-code-6' });
            }

        })
    </script>
}
